package com.lvtongka.app.fragment.home

import android.text.TextUtils
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.lvtongka.app.base.BaseViewModel

import com.lvtongka.app.http.OkhttpUtils
import com.lvtongka.app.common.bean.AccountRequest
import com.lvtongka.app.common.bean.RealNameRequest
import com.lvtongka.app.fragment.common.bean.SetPayPwdRequest
import com.lvtongka.app.fragment.home.bean.HomeMallList
import com.lvtongka.app.fragment.home.bean.HomeMallPage
import com.lvtongka.app.fragment.home.bean.HomeMallRequest
import com.lvtongka.app.fragment.home.bean.HomeMallResponse
import com.lvtongka.app.fragment.home.bean.OfflineData
import com.lvtongka.app.fragment.home.bean.OfflineData2
import com.lvtongka.app.fragment.home.bean.OfflineRequest
import com.lvtongka.app.fragment.home.bean.OfflineResponse
import com.lvtongka.app.fragment.home.bean.OfflineResponse2
import com.lvtongka.app.fragment.home.bean.OnlineData
import com.lvtongka.app.fragment.home.bean.OnlineRequest
import com.lvtongka.app.fragment.home.bean.OnlineResponse
import com.lvtongka.app.fragment.pay.bean.OpenScanPayRequest
import com.lvtongka.app.fragment.pay.bean.ScanDynamicsQrCodeData
import com.lvtongka.app.fragment.pay.bean.ScanDynamicsQrCodeRequest
import com.lvtongka.app.fragment.pay.bean.ScanDynamicsQrCodeResponse
import com.lvtongka.app.fragment.pay.bean.ScanQrCodeData
import com.lvtongka.app.fragment.pay.bean.ScanQrCodeRequest
import com.lvtongka.app.fragment.pay.bean.ScanQrCodeResponse
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.HttpCallBack
import com.lvtongka.app.http.common.UrlConstants
import com.lvtongka.app.ui.qr.PaymentCodeViewModel
import com.lvtongka.app.ui.setting.SettingViewModel
import com.lvtongka.app.ui.welfare.WelfareItem
import com.lvtongka.app.ui.welfare.WelfareResponse

class HomeViewModel : BaseViewModel() {
    private val _realName = MutableLiveData<Boolean>()
    val realName:LiveData<Boolean> = _realName

    private val _setPwd = MutableLiveData<Boolean>()
    val setPwd:LiveData<Boolean> = _setPwd

    private val _welfareItemList = MutableLiveData<List<WelfareItem>>()
    val welfareItemList:LiveData<List<WelfareItem>> = _welfareItemList

    private val _couponCount = MutableLiveData<Int>()
    val couponCount: LiveData<Int> = _couponCount

    private val _homeMallPage = MutableLiveData<HomeMallPage>()
    val homeMallPage: LiveData<HomeMallPage> = _homeMallPage

    private val _scanQrCodeData = MutableLiveData<ScanQrCodeData>()
    val scanQrCodeData:LiveData<ScanQrCodeData> = _scanQrCodeData

    private val _scanDynamicsQrCodeData = MutableLiveData<ScanDynamicsQrCodeData>()
    val scanDynamicsQrCodeData:LiveData<ScanDynamicsQrCodeData> = _scanDynamicsQrCodeData

    private val _offlineData = MutableLiveData<OfflineData>()
    val offlineData:LiveData<OfflineData> = _offlineData

    private val _offlineData2 = MutableLiveData<OfflineData2>()
    val offlineData2:LiveData<OfflineData2> = _offlineData2

    private val _onlineData = MutableLiveData<OnlineData>()
    val onlineData:LiveData<OnlineData> = _onlineData

    private val _onlineData2 = MutableLiveData<OnlineData>()
    val onlineData2:LiveData<OnlineData> = _onlineData2

    private val _openScanPay = MutableLiveData<Boolean>()
    val openScanPay: LiveData<Boolean> = _openScanPay

    // 识别商户静态二维码
    fun scanQrCode(qrCode: String) {
        val request = ScanQrCodeRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val scanQrCodeResponse = Gson().fromJson(result, ScanQrCodeResponse::class.java)
                    _scanQrCodeData.postValue(scanQrCodeResponse.data)
                }
            }, getAccessToken())
    }

    // 识别商户动态二维码
    fun scanDynamicsQrCode(qrCode: String) {
        val request = ScanDynamicsQrCodeRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val scanQrCodeResponse = Gson().fromJson(result, ScanDynamicsQrCodeResponse::class.java)
                    _scanDynamicsQrCodeData.postValue(scanQrCodeResponse.data)
                }
            }, getAccessToken())
    }

    // 龙城福利券
    fun getWelfareList() {
        val request = HashMap<String, String>()
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_COUPON, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val welfareResponse = Gson().fromJson(result, WelfareResponse::class.java)
                    _welfareItemList.postValue(welfareResponse.data.list)
                }
            }, getAccessToken())
    }

    // 实名认证
    fun realNameAuthentication(name: String, idNo: String) {
        val  request = RealNameRequest(name, idNo)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, true)
                    _realName.postValue(true)
                }
            }, getAccessToken())
    }

    // 设置密码
    fun setPayPassword(pwd: String) {
        val  request = SetPayPwdRequest(pwd)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    SPHelper.putBoolean(SPKConstants.IS_SET_PWD, true)
                    _setPwd.postValue(true)
                }
            }, getAccessToken())
    }

    // 查询线上商城
    fun queryMall(type: Int) {
        val  request = when(type) {
            1 -> HomeMallRequest(hotShop = "1")
            2 -> HomeMallRequest(siftShop = "1")
            3 -> HomeMallRequest(localShop = "1")
            else -> {HomeMallRequest()}
        }
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_MALL, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val response = Gson().fromJson(result, HomeMallResponse::class.java)
                    _homeMallPage.postValue(response.data.page)
                }
            }, getAccessToken())
    }

    // 查询线下好店分类 7 11
    fun queryOfflineClass() {
        val request = AccountRequest("80")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val offlineResponse = Gson().fromJson(result, OfflineResponse::class.java)
                    _offlineData.postValue(offlineResponse.data)
                }
            }, getAccessToken())
    }

    fun queryOffline(selectedClassify: String) {
        val request = OfflineRequest(selectedClassify)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val offlineResponse = Gson().fromJson(result, OfflineResponse2::class.java)
                    _offlineData2.postValue(offlineResponse.data)
                }
            }, getAccessToken())
    }

    fun checkAuthorize(mallList: HomeMallList) {
        val m = if (TextUtils.isEmpty(mallList.mallId)) {
            mallList.ltPay
        } else {
            mallList.mallId
        }
        val request = OnlineRequest(m, mallList.mallPayType, mallList.homePage)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_CHECK_AUTHORIZE, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val onlineResponse = Gson().fromJson(result, OnlineResponse::class.java)
                    _onlineData.postValue(onlineResponse.data)
                }
            }, getAccessToken())
    }

    fun authorize(mallList: HomeMallList) {
        val m = if (TextUtils.isEmpty(mallList.mallId)) {
            mallList.ltPay
        } else {
            mallList.mallId
        }
        val request = OnlineRequest(m, mallList.mallPayType, mallList.homePage)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_AUTHORIZE, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val onlineResponse = Gson().fromJson(result, OnlineResponse::class.java)
                    _onlineData2.postValue(onlineResponse.data)
                }
            }, getAccessToken())
    }

    // 开通扫码支付
    fun openScanPay(pwd: String) {
        val request = if (TextUtils.isEmpty(pwd)) {
            OpenScanPayRequest("", "0", "3")
        } else {
            OpenScanPayRequest(pwd, "1", "3")
        }
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _openScanPay.postValue(true)
                }
            }, getAccessToken())
    }
}